<?php

namespace App\Admin\Actions;

use Dcat\Admin\Actions\Action;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;

class CacheManage extends Action
{
    /**
     * @return string
     */
    protected $title = 'Title';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        // dump($request->input('action'));
        $action = $request->input('action');
        switch ($action) {
            case 'view':
                Artisan::call('view:cache');
                break;
            case 'config':
                Artisan::call('config:cache');
                break;
            case 'event':
                Artisan::call('event:cache');
                break;
            case 'route':
                Artisan::call('route:cache');
                break;
            default:
                // 清理默认缓存
                Artisan::call('view:clear');
                Artisan::call('config:clear');
                Artisan::call('event:clear');
                Artisan::call('route:clear');
                Artisan::call('cache:clear');
        }

        return $this->response()->success('操作成功')->redirect('/');
    }


    public function html()
    {
        return <<<HTML

<ul class="nav navbar-nav ml-1">
    <li class="dropdown dropdown-language nav-item">
        <a class="dropdown-toggle nav-link pr-1" href="#" id="dropdown-flag" data-toggle="dropdown">
            <i class="fa fa-fw fa-lg feather icon-server"></i>
        </a>
        <ul class="dropdown-menu" aria-labelledby="dropdown-flag">

            <li class="dropdown-item {$this->getElementClass()}" data-action="view">
                <a class="py-1"><i class="fa fa-fw fa-lg fa-circle-o"></i> 缓存视图</a>
            </li>

            <li class="dropdown-item {$this->getElementClass()}" data-action="config">
                <a class="py-1"><i class="fa fa-fw fa-lg fa-circle-o"></i> 缓存配置</a>
            </li>

            <li class="dropdown-item {$this->getElementClass()}" data-action="event">
                <a class="py-1"><i class="fa fa-fw fa-lg fa-circle-o"></i> 缓存事件</a>
            </li>

            <li class="dropdown-item {$this->getElementClass()}" data-action="route">
                <a class="py-1"><i class="fa fa-fw fa-lg fa-circle-o"></i> 缓存路由</a>
            </li>

            <li class="dropdown-divider"></li>

            <li class="dropdown-item {$this->getElementClass()}" data-action="clear">
                <a class="py-1"><i class="fa fa-fw fa-lg fa-circle-o"></i> 清理缓存</a>
            </li>
        </ul>
    </li>
</ul>

HTML;

    }

    /**
     * @return string|array|void
     */
    public function confirm()
    {
        // return ['Confirm?', 'contents'];
    }

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [];
    }
}
